﻿using Common.Device.DeviceDb;
using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.Models;
using Common.Device.ViewModels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static Org.BouncyCastle.Crypto.Digests.SkeinEngine;

namespace Common.Device.Views
{
    /// <summary>
    /// FctView.xaml 的交互逻辑
    /// </summary>
    public partial class FctView : UserControl
    {
        public FctViewModel viewModel = new FctViewModel();
        public FctView(UseDevice device, Order order = null)
        {
            InitializeComponent();
            //根据设备参数2 加载继电器和电平采集数量 （继电器;电平）
            if (!string.IsNullOrEmpty(device.parameter2))
            {
                string[] channel = device.parameter2.Split(new char[] { ';' ,','},StringSplitOptions.RemoveEmptyEntries);
                if (channel.Length >= 1)
                {
                    jdq = int.Parse(channel[0]);
                    viewModel.HasJdq = true;
                }
                if (channel.Length >= 2)
                {
                    var channels = new System.Collections.ObjectModel.ObservableCollection<int> { };
                    for (int i = 1; i < int.Parse(channel[1])+1; i++)
                    {
                        channels.Add(i);
                    }
                    viewModel.DpChannels = channels;
                    viewModel.HasDp = true;
                }
            }
            switch (device.model)
            {
                case DeviceEnum.DeviceModelEnum.FctModBus3: viewModel.HasDp = false; break;
                default:
                    break;
            }
            if (order != null)
            {
                //根据测试指令刷新界面
                Enum.TryParse(order.temp, out FctEnum.TestMode mode);
                Fct parameter = JsonConvert.DeserializeObject<Fct>(order.deviceModel);
                switch (mode)
                {
                    case FctEnum.TestMode.继电器控制:
                        viewModel.MenuIndex = 0;
                        viewModel.OpenChannel = parameter.断开通道;
                        viewModel.CloseChannel = parameter.闭合通道;
                        break;
                    case FctEnum.TestMode.电平采集:
                        viewModel.MenuIndex = 1;
                        viewModel.DpChannel = parameter.电平通道;
                        viewModel.DpState = parameter.期望状态;
                        break;
                    default:
                        break;
                }
            }
            DataContext = viewModel;
        }
        //仪器对象
        public object Device;
        //public Interfaces.IFct Device;
        //继电器数量
        public int jdq;
        private void JdqClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            Fct parameter = new Fct()
            {
                mode=FctEnum.TestMode.继电器控制,
                断开通道 = viewModel.OpenChannel,
                闭合通道 = viewModel.CloseChannel,
            };
            if (parameter.GetTextChannel(parameter.断开通道) == null)
            {
                View.Controls.Dialogs.TipView.Show("断开通道输入有误!");
                return;
            }
            if (parameter.GetTextChannel(parameter.闭合通道) == null)
            {
                View.Controls.Dialogs.TipView.Show("闭合通道输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder((Device as IDevice).Name));
            }
        }
        private void DpClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            Fct parameter = new Fct()
            {
                mode = FctEnum.TestMode.电平采集,
                电平通道 = viewModel.DpChannel,
                期望状态 = viewModel.DpState,
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder((Device as IDevice).Name));
            }
        }
    }
}
